package com.cuitrip.business.order.detail.fetcher;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.app.base.CtApiCallback;
import com.cuitrip.app.base.CtException;
import com.cuitrip.app.rong.RongCloudEvent;
import com.cuitrip.business.order.detail.orderstatus.IOrderFetcher;
import com.cuitrip.business.order.detail.ui.IOrderDetailView;
import com.cuitrip.business.order.model.OrderItem;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;

import io.rong.imlib.model.Conversation;
import io.rong.message.InformationNotificationMessage;

/**
 * Created by ak on 15/12/17.
 */
public class OrderFetcher implements IOrderFetcher {

    private IOrderDetailView orderDetailView;

    public OrderFetcher(IOrderDetailView orderDetailView) {
        this.orderDetailView = orderDetailView;
    }

    IProxyCallback iProxyCallback = new IProxyCallback() {

        @Override
        public boolean onProxyResult(ProxyResult<?> result) {
            CtApiResponse response = (CtApiResponse) result.getData();
            if (ApiManager.CANCEL_ORDER.equals(response.getApiName())){
                orderDetailView.hideLoading();
                if (response.isResponseNormal()){
                    refuseCallback.onSuc();
                }else{
                    refuseCallback.onFailed(new CtException(response.msg));
                }
            }else if (ApiManager.CONFIRM_ORDER.equals(response.getApiName())){
                orderDetailView.hideLoading();
                if (response.isResponseNormal()){
                    confirmCallback.onSuc();
                }else{
                    confirmCallback.onFailed(new CtException(response.msg));
                }
            } else if (ApiManager.BEGIN_ORDER.equals(response.getApiName())){
                orderDetailView.hideLoading();
                if (response.isResponseNormal()){
                    startCallback.onSuc();
                }else{
                    startCallback.onFailed(new CtException(response.msg));
                }
            } else if (ApiManager.END_ORDER.equals(response.getApiName())){
                orderDetailView.hideLoading();
                if (response.isResponseNormal()){
                    endCallback.onSuc();
                }else{
                    endCallback.onFailed(new CtException(response.msg));
                }
            }
            return false;
        }
    };

    private ApiProxy apiProxy = new ApiProxy(iProxyCallback);


    private CtApiCallback refuseCallback;

    private CtApiCallback confirmCallback;

    private CtApiCallback startCallback;

    private CtApiCallback endCallback;

    @Override
    public void refuseOrder(OrderItem orderItem, final CtApiCallback callback) {
        this.refuseCallback = callback;
        orderDetailView.showLoading();
        OrderBusiness.refuseOrder(apiProxy, orderItem.getOid());
    }

    @Override
    public void confirmOrder(OrderItem orderItem, final CtApiCallback callback) {
        this.confirmCallback = callback;
        orderDetailView.showLoading();
        OrderBusiness.confirmOrder(apiProxy, orderItem.getOid());
    }

    @Override
    public void startOrder(OrderItem orderItem, final CtApiCallback callback) {
        orderDetailView.showLoading();
        this.startCallback = callback;
        OrderBusiness.beginOrder(apiProxy, orderItem.getOid());
    }

    @Override
    public void endOrder(OrderItem orderItem, final CtApiCallback callback) {
        orderDetailView.showLoading();
        this.endCallback = callback;
        OrderBusiness.endOrder(apiProxy, orderItem.getOid());
    }

}
